/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.controllers;

import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TrayItem;
import org.toedo.logging.Log;
import org.toedo.preferences.PreferenceConstants;
import org.toedo.utils.Constants;
import org.toedo.Activator;

public class TrayController {
	
	private Shell mShell;
	
	private TrayItem mApplicationItem;

	private Image mApplicationImage;
	
	private boolean mIsApplicationHidden = false;
	private boolean mIsTrayShown = false;
	
	/**
	 * Holder for singleton implementation.
	 */
	private static class TrayControllerHolder {
		private static final TrayController INSTANCE = new TrayController();
	}
	
	/**
	 * Get the unique instance of the TrayController.
	 * @return The instance of the TrayController
	 */
	public static TrayController getInstance() {
		return TrayControllerHolder.INSTANCE;
	}
	
	private TrayController() {
		Activator.getDefault();
		mApplicationImage = Activator.getImageDescriptor("/icons/application-16.png").createImage(); //$NON-NLS-1$
		
		mShell = null;
		
		Controller.getInstance().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {

			private boolean isTrayProperty(PropertyChangeEvent event) {
				String property = event.getProperty();
				return (property.equals(PreferenceConstants.TRAY_SHOW_TRAY_ICON));
			}
			
			@Override
			public void propertyChange(PropertyChangeEvent event) {
				if (isTrayProperty(event)) {
					if (mIsTrayShown) {
						hideTrayItems();
					}
					if (Controller.getInstance().getPreferenceStore().getBoolean(PreferenceConstants.TRAY_SHOW_TRAY_ICON)) {
						showTrayItems();
					}
				}				
			}
			
		});
		
		Log.getInstance(TrayController.class).debug("TrayController initialized."); //$NON-NLS-1$
	}
	
	public void setShell(Shell shell) {
		mShell = shell;
	}
	
	private TrayItem createTrayItem(String toolTip, Image image) {
		TrayItem result = new TrayItem(Display.getDefault().getSystemTray(), SWT.NONE);
		result.setToolTipText(toolTip);
		result.setImage(image);
		return result;
	}
	
	public void showTrayItems() {
		mApplicationItem = createTrayItem(Constants.APP_NAME, mApplicationImage);
		mApplicationItem.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (mIsApplicationHidden) {
					showApplication();
				} else {
					hideApplication();
				}
			}
		});
		
		mIsTrayShown = true;
	}
	
	private void hideTrayItems() {
		mApplicationItem.dispose();
		mIsTrayShown = false;
	}
	
	private void showApplication() {
		mShell.setVisible(true);
		mShell.setActive();
		mShell.setFocus();
		mShell.setMinimized(false);
		mIsApplicationHidden = false;
	}
 
	public void hideApplication() {
		mShell.setVisible(false);
		mIsApplicationHidden = true;
	}
	
	public boolean isTrayShown() {
		return mIsTrayShown;
	}
	
	public void dispose() {
		mApplicationImage.dispose();
	}
}
